<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace app\shop\api;

use app\common\api\BaseApi;
use app\shop\model\ProductGroup;
use app\extend\Page;

class ProductGroupApi extends  BaseApi{

    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "shop/ProductGroupApi/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "shop/ProductGroupApi/add";
    /**
     * 保存
     */
    const SAVE = "shop/ProductGroupApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "shop/ProductGroupApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "shop/ProductGroupApi/delete";

    /**
     * 查询
     */
    const QUERY = "shop/ProductGroupApi/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "shop/ProductGroupApi/getInfo";

    const QUERY_WITH_PRODUCT="shop/ProductGroupApi/queryWithProduct";


    /**
     * 获取商品分组信息
     */
    const GET_GROUP_BY_ID = "shop/ProductGroupApi/getGroupsById";
    const GET_END_TIME = "shop/ProductGroupApi/getEndTime";

	protected function _init(){
		$this->model = new ProductGroup();
	}

    public function getEndTime($gid){
        $model = new \think\Model();
        $r = $model->query("select max('end_time') as end_time from itboye_product_group where g_id=".$gid.' and end_time>'.time());
        if(false === $r) return $this->apiReturnErr($mdoel->getDbError());
        return $this->apiReturnSuc($r[0]);
    }

    public function queryWithProduct($map = null,$page = ['curpage'=>1,'size' =>10],$order = false,$params = false,$field = false){

        $main_img_type = 6015;//主图

        $query = $this->model->alias('g')->join("left join itboye_product as p on p.id = g.p_id")->join('(select img_id,pid from itboye_product_image where type='.$main_img_type.') as image on g.p_id=image.pid','left')->join('(select max(ori_price) as ori_price,product_id from itboye_product_sku group by product_id) as sku on g.p_id=sku.product_id','inner')->join('itboye_product_attr on g.p_id=itboye_product_attr.pid','left')->join('common_datatree as dt on dt.code=p.dt_origin_country and dt.parentid=35','left')->group('g.p_id');

        $query = $query -> page($page['curpage'] . ',' . $page['size']);
        if(isset($map['cate_id'])){
            $p = $map['cate_id'][1]; //bug
            unset($map['cate_id']);
            $map['cate_id'] = ['in',$p];
        }
        if(null !== $map)    $query = $query -> where($map);
        if(false !== $order) $query = $query -> order($order);
        if(false !== $field) $query = $query -> field($field);

        $list = $query -> select();
        // echo $query->getLastSql();exit;
        if (false === $list) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }
        //count
        $query = $this->model->alias('g')->join("left join itboye_product as p on p.id = g.p_id")->join('(select img_id as img,pid from itboye_product_image where type='.$main_img_type.') as image on g.p_id=image.pid','left')->join('(select min(price) as price,product_id from itboye_product_sku group by product_id) as sku on g.p_id=sku.product_id','inner')->join('itboye_product_attr on g.p_id=itboye_product_attr.pid','left');

        if($map) $query = $query -> where($map);
        $count = $query -> count();
        // $Page = new Page($count, $page['size']);
        //分页跳转的时候保证查询条件
        // if (false !== $params) {
        //     foreach ($params as $key => $val) {
        //         $Page -> parameter[$key] = urlencode($val);
        //     }
        // }
        // $show = $Page -> show();

        return $this -> apiReturnSuc(["count" => $count,"list" => $list]);
    }

    public function getGroupsById($pid){

        $map = ['p_id' => $pid];
        $now = time();
        $map['_string'] = 'start_time <='.$now.' and end_time >='.$now;
        // $map['start_time'] = ['ELT',$now];
        // $map['end_time']   = ['EGT',$now];

        $result = $this -> model -> join("left join common_datatree on g_id = common_datatree.id") -> where($map) -> field("p_id,g_id,sku_id,price,start_time,end_time,common_datatree.name as name") -> select();

        if(false === $result){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }
        return $this -> apiReturnSuc($result);

    }

}

